package com.itheima.dao.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class AccountDaoImpl implements IAccountDao {


/*xml文件中  set注入bean*/
    private JdbcTemplate jdbcTemplate;

      //有set方法 配置文件中可以注入bean
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }



    public Account findAccountById(Integer account) {
        List<Account> list = jdbcTemplate.query("select * from account where id=?",
                new BeanPropertyRowMapper<Account>(Account.class),account);

        return list.isEmpty()?null:list.get(0);
    }

    public Account findAccountByName(String accountName) {
        List<Account> list = jdbcTemplate.query("select * from account where name=?",
                new BeanPropertyRowMapper<Account>(Account.class),accountName);
        if(list.isEmpty()){
            return null;
        }
        if(list.size()>1){
            throw new RuntimeException("结果集不唯一");

        }
        return list.get(0);

    }

    public void updateAccount(Account account) {
    jdbcTemplate.update("update account set name=?,money=? where id=?",
        account.getName(),account.getMoney(),account.getId());
    }

    public void transfer(String inName, String outName, Float money) {

    }
}
